package com.leha.api.controller;

import com.leha.core.entity.mybatis.LhUser;
import com.leha.core.services.ErrorMessageServices;
import com.leha.core.services.GameServices;
import constants.APIErrorDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import utils.APIResult;

import javax.ws.rs.FormParam;
import javax.ws.rs.QueryParam;

@RestController
@Api(value = "/game", description = "游戏相关API")
@RequestMapping("/game")
@ResponseStatus(HttpStatus.OK)
public class GameController {
    @Autowired
    GameServices gameServices;

    @Autowired
    private ErrorMessageServices errorMessageServices;

    @RequestMapping(value = "/{gameId}/settlement", method = {RequestMethod.POST})
    @ApiOperation(value = "结算", notes = "游戏结束，结算数据")
    APIResult GameSettlement(@PathVariable Integer gameId,
                    @FormParam(value = "uid") Integer uid,
                    @FormParam(value = "osUid") Integer osUid,
                    @FormParam(value = "score") Integer score,
                    @FormParam(value = "osScore") Integer osScore,
                    @FormParam(value = "winner") Integer winner) {
        if (uid == null || uid==0) {
            return new APIResult(APIErrorDict.PARAMERROR);
        }
        return new APIResult(gameServices.addGameRecord(gameId, uid, osUid, score, osScore, winner));
    }

    @RequestMapping(value = "/{gameId}/dailyRank", method = {RequestMethod.GET})
    @ApiOperation(value = "游戏日排行", notes = "获取排名数据")
    APIResult GetGameDailyRank(@PathVariable Integer gameId, @QueryParam(value="uid") Integer uid){
        return new APIResult(gameServices.getDailyRank(gameId, 100, uid));
    }

    @RequestMapping(value = "/{gameId}/weeklyRank", method = {RequestMethod.GET})
    @ApiOperation(value = "游戏周排行", notes = "获取排名数据")
    APIResult GetGameWeeklyRank(@PathVariable Integer gameId, @QueryParam(value="uid") Integer uid){
        return new APIResult(gameServices.getDailyRank(gameId, 100, uid));
    }

    @RequestMapping(value = "/{gameId}/timesRank", method = {RequestMethod.GET})
    @ApiOperation(value = "游戏毅力排行榜", notes = "获取排名数据")
    APIResult GetGameTimesRank(@PathVariable Integer gameId, @QueryParam(value="uid") Integer uid){
        return new APIResult(gameServices.getTimesRank(gameId, 100, uid));
    }

    @RequestMapping(value = "/{gameId}/rewardsRank", method = {RequestMethod.GET})
    @ApiOperation(value = "游戏奖品排行榜", notes = "获取排名数据")
    APIResult GetGameRewardsRank(@PathVariable Integer gameId, @QueryParam(value="uid") Integer uid){
        return new APIResult(gameServices.getRewardsRank(gameId, 100, uid));
    }

    @RequestMapping(value = "/{gameId}/rewardsNotice", method = {RequestMethod.GET})
    @ApiOperation(value = "获取游戏奖励公告", notes = "玩家的奖励")
    APIResult GetGameRewardsNotice(@PathVariable Integer gameId){
        return new APIResult(gameServices.getGameRewards(gameId));
    }

    @RequestMapping(value = "/{gameId}/rewardsList", method = {RequestMethod.GET})
    @ApiOperation(value = "获取游戏奖励列表", notes = "玩家的奖励")
    APIResult GetGameRewardsList(@PathVariable Integer gameId){
        return new APIResult(gameServices.getRewardItem(gameId));
    }

    @RequestMapping(value = "/reloadSrv", method = {RequestMethod.GET})
    @ApiOperation(value = "重新加载服务", notes = "重新加载服务")
    APIResult GameSettlement() {
        gameServices.Reload();
        return new APIResult("OK");
    }
}
